package com.huika.renrench.control.my.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.huika.lib.utils.MoneyShowTool;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.my.account.PropertyTotalActivity;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.AccountPropertyBean;
import com.huika.renrench.view.TitleBarHelper;
/**
 * @author chenyuan
 * @date：2015年6月2,下午2:15:30
 * @describe:账户资产activity
 */
public class AccountPropertyActivity extends BaseAct implements Listener<RequestResult<AccountPropertyBean>> {
	private AccountPropertyBean accountPropertyBean;
	/**账户总资产*/
	private TextView text_total_account_assets;
	/**可用余额*/
	private TextView text_can_use_num;
	/**冻结余额*/
	private TextView text_dongjie_num;
	/**投资金额*/
	private TextView text_touzi_num;
	
	
	@Override
	public void setRootView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_account_property);
		TitleBarHelper titleBarHelper=new TitleBarHelper(this, R.string.common_null, R.string.common_null, R.string.common_null);
		titleBarHelper.setTitleMsg("账户资产");
		titleBarHelper.setLeftMsg("我的");
		findViewById(R.id.property_totalLoy).setOnClickListener(this);
		text_total_account_assets=(TextView) findViewById(R.id.text_total_account_assets);
		text_can_use_num=(TextView) findViewById(R.id.text_can_use_num);
		text_dongjie_num=(TextView) findViewById(R.id.text_dongjie_num);
		text_touzi_num=(TextView) findViewById(R.id.text_touzi_num);
		
		
		/**获取网络数据*/
		getDataFromNet();
		
	}

	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		super.widgetClick(v);
		if (v.getId()==R.id.property_totalLoy) {
			Intent intetn=new Intent(this, PropertyTotalActivity.class);
			intetn.putExtra("accountPropertyBean", accountPropertyBean);
			showActivity(this,intetn);
		}
	}
	
	private void getDataFromNet() {
		// TODO Auto-generated method stub
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		getUserId(ajaxParams);
		FormResultRequest<AccountPropertyBean> getResultRequest = new FormResultRequest<AccountPropertyBean>(
				UrlConstants.GETUSERTOTALAMOUNT, this, this,
				new TypeToken<RequestResult<AccountPropertyBean>>() {
				}.getType());
		getResultRequest.setRequestParams(ajaxParams);
		executeRequest(getResultRequest);
	}
	
	private void getUserId(JSONWrapAjaxParams ajaxParams) {
		String u_id = GlobalApp.getInstance().getUserId();
		if (!TextUtils.isEmpty(u_id)) {
			ajaxParams.putCommonTypeParam("userId", u_id);
		} else {
			ajaxParams.putCommonTypeParam("userId", "0");
		}
	}

	@Override
	public void onResponse(RequestResult<AccountPropertyBean> result) {
		// TODO Auto-generated method stub
		if (result.flag == 1) {
			if (result != null && result.getRs() != null
					) {
				accountPropertyBean=result.getRs();
				setData();
			} else {
			}
		} else {
			showToastMsg(result.msg);
		}
		dismissLoadingDialog();
	}

	
	private void setData() {
		text_total_account_assets.setText("¥ "+String.valueOf(MoneyShowTool.twolastDF(accountPropertyBean.rechargeAmount-accountPropertyBean.withdrawAmount+accountPropertyBean.investRevenue-accountPropertyBean.revenueFeeAmount)));
		text_can_use_num.setText("¥ "+String.valueOf(MoneyShowTool.twolastDF(accountPropertyBean.rechargeAmount-accountPropertyBean.withdrawAmount+accountPropertyBean.investRevenue-accountPropertyBean.frozenBalance-accountPropertyBean.investAmount-accountPropertyBean.revenueFeeAmount)));
		text_dongjie_num.setText("¥ "+String.valueOf(MoneyShowTool.twolastDF(accountPropertyBean.frozenBalance)));
		text_touzi_num.setText("¥ "+String.valueOf(MoneyShowTool.twolastDF(accountPropertyBean.investAmount)));
	}
}
